(function(){
    var util = require('common:widget/ui/libs/util.js').util;

    P.createPage({
        _create:function(){
            var $page = $('.page-assetsdemand');
            this.data({
                '$page':$page,
                '$profitBlock':$page.find('.profit-block')
            });
            this.getProfitList();
        },

        _init:function(){
            var $profitBlock = this.data('$profitBlock');
            $profitBlock.find('.type-tab li a').click(function(){
                $profitBlock.find('.type-tab li a').removeClass('active');
                $(this).addClass('active');
                if($(this).attr('data-type') == 'accumulate'){
                    $profitBlock.find('.day-profit').hide();
                    $profitBlock.find('.accumulate-profit').show();
                }else{
                    $profitBlock.find('.accumulate-profit').hide();
                    $profitBlock.find('.day-profit').show();
                }
            })
        },

        getProfitList:function(){
            var me = this,
                $dayContainer = this.data('$profitBlock').find('.day-profit'),
                $accumulateContainer = this.data('$profitBlock').find('.accumulate-profit');
            $.get('/user/getprofitlist',{
               pid:1
            },function(ret,status){
                if(ret.error_no == 0){
                    var date = [],
                        profitList = [],
                        accumulateList = [];
                    $.each(ret.data,function(k,v){
                        date.push(util.toTime(parseFloat(v.date)*1000,'MM-dd'));
                        profitList.push(parseFloat(v.profit));
                        accumulateList.push(parseFloat(v.profit_accumulate))
                    });
                    me.createSpline($dayContainer,'我的每日收益趋势',date,profitList);
                    me.createSpline($accumulateContainer,'我的累计收益趋势',date,accumulateList);
                }
            },'json');
        },


        createSpline:function($root,title,date,list){

            $root.highcharts({
                chart:{
                    width:700,
                    height:280,
                    backgroundColor:'#fff'
                },
                credits : {
                    enabled:false
                },
                title: {
                    text: ' ',
                    x: -20 //center
                },
                legend: {
                    align: 'center',
                    verticalAlign: 'top',
                    x: 200,
                    y: 0
                },
                xAxis: {
                    type:'datetime',
                    categories: date,
                    gridLineWidth: 1 ,//图形的背景线设置
                    lineWidth : 1 //Y轴的宽度
                },
                yAxis: {
                    gridLineWidth: 1 ,//图形的背景线设置
                    lineWidth : 1, //Y轴的宽度
                    title: {
                        text: title
                    },
                    // categories : [0,10,20]
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    valuePrefix: '￥'
                },
                plotOptions : {
                    series: {
                        fillOpacity: 0.2
                    }
                },
                series: [{
                    type : 'line',
                    name: '我的收益',
                    data: list,
                    color:'#F93'
                }]
            });
        }
    });
})();